﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using NLog;

namespace Zhuang.WindowsService
{
    public class GlobalJobListener : IJobListener
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        public GlobalJobListener()
        {
        }

        public void JobExecutionVetoed(JobExecutionContext context)
        {
            log.Info("Job: " + context.JobDetail.Name + " was vetoed.");
        }

        public void JobToBeExecuted(JobExecutionContext context)
        {
            log.Info("Job: " + context.JobDetail.Name + " start execute.");
        }

        public void JobWasExecuted(JobExecutionContext context, JobExecutionException jobException)
        {
            log.Info("Job: " + context.JobDetail.Name + " was executed.");
            if (jobException != null && jobException.Message != null)
            {
                log.Error(FormatExceptionMessage(jobException));
            }
        }

        private string FormatExceptionMessage(JobExecutionException jee)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(jee.Message);
            sb.AppendLine();
            sb.Append(jee.StackTrace);
            sb.AppendLine();
            sb.Append(jee.InnerException.StackTrace);

            return sb.ToString();
        }

        public string Name
        {
            get { return "Import Service Listener"; }
        }
    }
}
